<?php

namespace app\xwam\model;

/**
 * ProductInvoicing 模型类
 *
 * @since 1.0 <2016-5-28> SoChishun <14507247@qq.com> Added.
 */
class ProductInvoicingModel extends AppbaseModel {

    protected $table = 't_porg_product_invoicing';

    // 保存出库 2016-5-30
    function saveInvoicing() {
        $rules = array(
            array('product_id', 'number', '商品无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('store_id', 'number', '分店无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('number', 'currency', '数量无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('price', 'currency', '价格无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('old_stock', 'double', '库存无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('user_name', 'require', '用户无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('site_id', 'number', 'SiteID无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        );
        if (!$this->validate($rules)->create()) {
            return $this->returnMsg(false, $this->getError());
        }
        $product_id = $this->data['product_id'];
        $number = floatval($this->data['number']);
        $price = floatval($this->data['price']);
        $amount = $number * $price;
        $this->subtotal = $amount;
        $this->new_stock = floatval($this->old_stock - $number);
        $this->new_stock = floatval($this->old_stock + $number);
        $result = $this->add();
        if (false !== $result) {
            // 更新商品表：进货数量, 进货金额, 当前库存
            $this->execute("update t_porg_product_goods set sale_quantity=sale_quantity+$number, sale_amount=sale_amount+$amount, stock=stock-$number where id=$product_id");
            $this->execute("update t_porg_product_goods set purchase_quantity=purchase_quantity+$number, purchase_amount=purchase_amount+$amount, stock=stock+$number where id=$product_id");
        }
        return $this->returnMsg($result);
    }

    function deleteInvoicing($id) {
        $result = $this->delete($id);
        return $this->returnMsg($result);
    }

    /**
     * 获取操作类型数据
     * @return type
     * @since 1.0 2017-3-22 SoChishun Added.
     */
    static function getOperateType($direct = '') {
        switch (trim($direct)) {
            case 'IN':
                $data = array('SELL-RETURN' => '销售退货(入库)', 'PURCHASE' => '采购入库', 'PURCHASE-RETURN' => '采购退货(入库)', 'INVENTORY-IN' => '盘点调仓(入库)');
                break;
            case 'OUT':
                $data = array('SALE' => '销售出库', 'INTERNAL' => '内部出库', 'INVENTORY-OUT' => '盘点调仓(出库)');
                break;
            default:
                $data = array('SELL-RETURN' => '销售退货(入库)', 'PURCHASE' => '采购入库', 'SALE' => '销售出库', 'PURCHASE-RETURN' => '采购退货(入库)', 'INTERNAL' => '内部出库', 'INVENTORY-IN' => '盘点调仓(入库)', 'INVENTORY-OUT' => '盘点调仓(出库)');
                break;
        }
        return $data;
    }

    /**
     * 获取出入库方向类型
     * @return type
     * @since 1.0 2017-3-22 SoChishun Added.
     */
    static function getDirectionType() {
        return array('IN' => '入库', 'OUT' => '出库');
    }

}
